package com.huishu.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huishu.commons.pojo.BasePage;
import com.huishu.commons.pojo.Result;
import com.huishu.commons.utlis.BeanUtils;
import com.huishu.goods.mapper.ProductCategoryMapper;
import com.huishu.goods.pojo.dto.ProductCategoryDTO;
import com.huishu.goods.pojo.model.ProductCategoryDO;
import com.huishu.goods.pojo.vo.ProductCategoryPageVO;
import com.huishu.goods.service.ProductCategoryService;
import com.huishu.goods.service.ProductLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author generator@ljj
 * @since 2023-04-08
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategoryDO> implements ProductCategoryService {

    @Autowired
    private ProductLabelService productLabelService;

    @Transactional
    @Override
    public Result handle(ProductCategoryDTO productCategoryDTO, int type) {
        List<ProductCategoryDO> list = new ArrayList<>();
        if (type == 1) {
            ProductCategoryDO productCategoryDO = BeanUtils.copyProperties(productCategoryDTO, ProductCategoryDO.class);
            list.add(productCategoryDO);
            if (productCategoryDO.getSort() == 1) {
                // 首位添加
                List<ProductCategoryDO> hotWordsDOList = this.list();
                hotWordsDOList.forEach(x -> {
                    x.setSort(x.getSort() + 1);
                    list.add(x);
                });
            } else {
                // 大于等于的都+1
                List<ProductCategoryDO> hotWordsDOList = this.list(Wrappers.<ProductCategoryDO>lambdaQuery()
                        .ge(ProductCategoryDO::getSort, productCategoryDTO.getSort()));
                // 如果添加3 原来的3 4 5 都加1变成 4 5 6
                hotWordsDOList.forEach(x -> {
                    x.setSort(x.getSort() + 1);
                    list.add(x);
                });
            }

        } else if (type == 2) {
            // 删除时 大于自身的都减去1
            // 删除3 原来的4 5 都-1变成 3 4
            LambdaQueryWrapper<ProductCategoryDO> queryWrapper = Wrappers.lambdaQuery(ProductCategoryDO.class)
                    .gt(ProductCategoryDO::getSort, productCategoryDTO.getSort());
            List<ProductCategoryDO> hotWordsDOList = this.list(queryWrapper);
            hotWordsDOList.forEach(x -> {
                if (!x.getId().equals(productCategoryDTO.getId())) {
                    x.setSort(x.getSort() - 1);
                    list.add(x);
                }
            });
            this.removeById(productCategoryDTO.getId());
        } else {
            // 修改
            ProductCategoryDO productCategoryDO = getById(productCategoryDTO.getId());
            if (productCategoryDTO.getSort().equals(productCategoryDO.getSort())) {
                list.add(BeanUtils.copyProperties(productCategoryDTO, ProductCategoryDO.class));
                // 不做处理
            } else if (productCategoryDTO.getSort() <= productCategoryDO.getSort()) {
                List<ProductCategoryDO> buoyList = this.list(Wrappers.<ProductCategoryDO>lambdaQuery()
                        .ge(ProductCategoryDO::getSort, productCategoryDTO.getSort())
                        .lt(ProductCategoryDO::getSort, productCategoryDO.getSort()));
                buoyList.forEach(x -> {
                    x.setSort(x.getSort() + 1);
                    list.add(x);
                });
                list.add(BeanUtils.copyProperties(productCategoryDTO, ProductCategoryDO.class));
            } else {
                List<ProductCategoryDO> buoyList = this.list(Wrappers.<ProductCategoryDO>lambdaQuery()
                        .gt(ProductCategoryDO::getSort, productCategoryDO.getSort())
                        .le(ProductCategoryDO::getSort, productCategoryDTO.getSort()));
                buoyList.forEach(x -> {
                    x.setSort(x.getSort() - 1);
                    list.add(x);
                });
                list.add(BeanUtils.copyProperties(productCategoryDTO, ProductCategoryDO.class));
            }
        }
        if(CollectionUtils.isEmpty(list) && type ==2){
            return Result.success();
        }
        boolean b = this.saveOrUpdateBatch(list);
        return b ? Result.success() : Result.fail();
    }

    @Override
    public Result pageList(BasePage basePage) {
        Page<ProductCategoryDO> page = new Page<>(basePage.getCurrPage(), basePage.getPageSize());
        LambdaQueryWrapper<ProductCategoryDO> queryWrapper = Wrappers.lambdaQuery(ProductCategoryDO.class)
                .like(StringUtils.isNotBlank(basePage.getSearch()), ProductCategoryDO::getName, basePage.getSearch())
                .orderByAsc(ProductCategoryDO::getSort);
        Page<ProductCategoryDO> productCategoryDOPage = this.baseMapper.selectPage(page, queryWrapper);
        ProductCategoryPageVO productCategoryPageVO = new ProductCategoryPageVO();
        productCategoryPageVO.setProductCategoryPage(productCategoryDOPage);
        // 查询
        productCategoryPageVO.setNewproductBestsellings(productLabelService.list());
        return Result.success(productCategoryPageVO);
    }
}
